﻿/************************************************************/
///         *                *                  *         ///
///       ***              * * *                ***       ///
///     *****            * * * * *              *****     ///
///   *******          * * * * * * *            *******   ///
/// *********        * * * * * * * * *          ********* ///
///   *******          * * * * * * *            *******   ///
///     *****            * * * * *              *****     ///
///       ***              * * *                ***       ///
///         *                *                  *         ///
/************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace RegularTool
{
    /// <summary>
    /// StringVerifyTool
    /// 创建人：njazy
    /// 创建时间：2021/8/23 14:18:20
    /// </summary>
    class StringVerifyTool
    {

        static System.Text.RegularExpressions.Regex regexChn = new System.Text.RegularExpressions.Regex("^[\u4e00-\u9fa5]+$");
        static System.Text.RegularExpressions.Regex regexNum = new System.Text.RegularExpressions.Regex("^-?\\d+$|^(-?\\d+)(\\.\\d+)?$");
        static System.Text.RegularExpressions.Regex regexEn = new System.Text.RegularExpressions.Regex("^[A-Za-z]+$");

        static System.Text.RegularExpressions.Regex regexAll = new System.Text.RegularExpressions.Regex("^[0-9a-zA-Z\u4E00-\u9FA5<]+$");
        static System.Text.RegularExpressions.Regex regexUnAllowable = new System.Text.RegularExpressions.Regex("[@#$*|/()\\[\\]{}'\"\\\\:?|=]");
        /// <summary>
        /// 不允许字符
        /// </summary>
        static readonly char[] unAllowableChars = { '%', '*', '=', '#', '\\', '\"', '\'', '$', '?', '>', ':', '|' };
        /// <summary>
        /// 过滤非法字符
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        internal static Boolean VerifyUnallowableChar(string str)
        {
            return regexUnAllowable.IsMatch(str);
        }

        /// <summary>
        /// 过滤非法字符
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        internal static string FilterUnallowableChar(string str)
        {
            return regexUnAllowable.Replace(str, "");
        }
        /// <summary>
        /// 电话号
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        internal static bool IsPhone(string input)
        {
            string pattern = "^\\(0\\d{2}\\)[- ]?\\d{8}$|^0\\d{2}[- ]?\\d{8}$|^\\(0\\d{3}\\)[- ]?\\d{7}$|^0\\d{3}[- ]?\\d{7}$";
            Regex regex = new Regex(pattern);
            return regex.IsMatch(input);
        }
        /// <summary>
        /// 判断输入的字符串是否是一个合法的手机号
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsMobilePhone(string input)
        {
            Regex regex = new Regex("^[1]+[3,5,8]+\\d{9}$");
            return regex.IsMatch(input);

        }
        /// <summary>
        /// 判断输入的字符串只包含数字
        /// 可以匹配整数和浮点数
        /// ^-?\d+$|^(-?\d+)(\.\d+)?$
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsNumber(string input)
        {
            string pattern = "^-?\\d+$|^(-?\\d+)(\\.\\d+)?$";
            Regex regex = new Regex(pattern);
            return regex.IsMatch(input);
        }
        /// <summary>
        /// 匹配非负整数
        ///
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsNotNagtive(string input)
        {
            Regex regex = new Regex(@"^\d+$");
            return regex.IsMatch(input);
        }
        /// <summary>
        /// 匹配正整数
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsUint(string input)
        {
            Regex regex = new Regex("^[0-9]*[1-9][0-9]*$");
            return regex.IsMatch(input);
        }
        /// <summary>
        /// 判断输入的字符串字包含英文字母
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsEnglisCh(string input)
        {
            Regex regex = new Regex("^[A-Za-z]+$");
            return regex.IsMatch(input);
        }


        /// <summary>
        /// 判断输入的字符串是否是一个合法的Email地址
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsEmail(string input)
        {
            string pattern = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
            Regex regex = new Regex(pattern);
            return regex.IsMatch(input);
        }


        /// <summary>
        /// 判断输入的字符串是否只包含数字和英文字母
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsNumAndEnCh(string input)
        {
            string pattern = @"^[A-Za-z0-9]+$";
            Regex regex = new Regex(pattern);
            return regex.IsMatch(input);
        }


        /// <summary>
        /// 判断输入的字符串是否是一个超链接
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsURL(string input)
        {
            //string pattern = @"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?";
            string pattern = @"^[a-zA-Z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$";
            Regex regex = new Regex(pattern);
            return regex.IsMatch(input);
        }


        /// <summary>
        /// 判断输入的字符串是否是表示一个IP地址
        /// </summary>
        /// <param name="input">被比较的字符串</param>
        /// <returns>是IP地址则为True</returns>
        public static bool IsIPv4(string input)
        {

            string[] IPs = input.Split('.');
            Regex regex = new Regex(@"^\d+$");
            for (int i = 0; i < IPs.Length; i++)
            {
                if (!regex.IsMatch(IPs[i]))
                {
                    return false;
                }
                if (Convert.ToUInt16(IPs[i]) > 255)
                {
                    return false;
                }
            }
            return true;
        }

    }
}
